package com.jess.arms.base.delegate.sys;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Cookie;

/**
 * <li>标题:获取Okhttp的缓存Cookie
 * <li>说明:
 * <li>创建人：邹旭
 * <li>创建日期：2019-09-18 11:28
 * <li>修改人:
 * <li>修改日期：
 * <li>修改内容：
 */
public class LoginStateUtil {
    /**
     * <li>说明:从序列化数据中解析Cookie
     * <li>创建时间:2019-09-18 11:37
     * <li>创建人: 邹旭
     * <li>修改人：
     * <li>修改日期：
     * <li>修改内容：
     *
     * @return
     */
    public static HashMap<String, List<Cookie>> getCachedOkhttpCookie() {
        HashMap<String, List<Cookie>> cookiesMap = new HashMap<>();
        HashMap<String, ArrayList<CookieWrapper>> cachedCookie = (HashMap<String, ArrayList<CookieWrapper>>) CacheDiskUtils.getInstance().getSerializable(ArmsConstant.CACHE_COOKIE);
        if (ObjectUtils.isNotEmpty(cachedCookie)) {
            Set<Map.Entry<String, ArrayList<CookieWrapper>>> set = cachedCookie.entrySet();
            for (Map.Entry<String, ArrayList<CookieWrapper>> entry : set) {
                String url = entry.getKey();
                if (ObjectUtils.isNotEmpty(entry.getValue())) {
                    List<Cookie> cookieList = new ArrayList<>();
                    for (CookieWrapper cookieWrapper : entry.getValue()) {
                        cookieList.add(cookieWrapper.getOkhttpCookie());
                    }
                    cookiesMap.put(url, cookieList);
                }
            }
        }

        return cookiesMap;
    }

    /**
     * <li>说明:缓存Cookie的数据(封装类)
     * <li>创建时间:2019-09-18 13:28
     * <li>创建人: 邹旭
     * <li>修改人：
     * <li>修改日期：
     * <li>修改内容：
     *
     * @param cookieStore
     */
    public static void cacheOkhttpCookie(HashMap<String, List<Cookie>> cookieStore) {
        HashMap<String, ArrayList<CookieWrapper>> cookieMap = new HashMap();
        Set<Map.Entry<String, List<Cookie>>> set = cookieStore.entrySet();
        for (Map.Entry<String, List<Cookie>> entry : set) {
            if (ObjectUtils.isNotEmpty(entry.getValue())) {
                ArrayList<CookieWrapper> cookieWrapperList = new ArrayList<>();
                for (Cookie cookie : entry.getValue()) {
                    CookieWrapper cookieWrapper = new CookieWrapper(cookie);
                    cookieWrapperList.add(cookieWrapper);
                }
                cookieMap.put(entry.getKey(), cookieWrapperList);
            }
        }
        CacheDiskUtils.getInstance().put(ArmsConstant.CACHE_COOKIE, cookieMap);
    }

    public static void clearOkhttpCookieCache() {
        CacheDiskUtils.getInstance().remove(ArmsConstant.CACHE_COOKIE);
    }

    /**
     * 注销登录之后
     */
    public static void logout() {
    }

    public static class CookieWrapper implements Serializable {
        public String domain;
        public String path;
        public String name;
        public String value;
        public long expiresAt;
        public boolean secure;
        public boolean httpOnly;
        public boolean persistent; // True if 'expires' or 'max-age' is present.

        public CookieWrapper() {
        }

        public CookieWrapper(Cookie cookie) {
            this.domain = cookie.domain();
            this.path = cookie.path();
            this.name = cookie.name();
            this.value = cookie.value();
            this.expiresAt = cookie.expiresAt();
            this.secure = cookie.secure();
            this.httpOnly = cookie.httpOnly();
            this.persistent = cookie.persistent();
        }

        public Cookie getOkhttpCookie() {
            Cookie.Builder builder = new Cookie.Builder().domain(domain)
                    .path(path)
                    .name(name)
                    .value(value)
                    .expiresAt(expiresAt);
            if (secure) {
                builder.secure();
            }
            if (httpOnly) {
                builder.httpOnly();
            }
            return builder.build();
        }
    }
}
